//
//  GPWithdrawalViewController.m
//  大股票
//
//  Created by mac on 2019/9/5.
//  Copyright © 2019 apple. All rights reserved.
//

#import "GPWithdrawalViewController.h"
#import "GPWithdrawalResultVC.h"
#import "GPAccountModel.h"
#import "DGPUtil.h"
#import "HHYPayAlertView.h"


@interface GPWithdrawalViewController ()
@property (weak, nonatomic) IBOutlet UITextField *moneyTextField;
@property (weak, nonatomic) IBOutlet UITextField *userTextField;
@property (weak, nonatomic) IBOutlet UITextField *bankNumTextField;

@end

@implementation GPWithdrawalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)submitAction:(id)sender {
    if (self.moneyTextField.text.length==0||self.userTextField.text.length==0||self.bankNumTextField.text.length==0) {
        [MBProgressHUD showInformation:@"请把信息填写完整！" toView:self.view andAfterDelay:HUD_TIME];
        return;
    }
    if ([self.moneyTextField.text floatValue]==0) {
        [MBProgressHUD showInformation:@"提现金额需要大于0" toView:self.view andAfterDelay:HUD_TIME];
        return;
    }
    if (self.bankNumTextField.text.length!=19) {
        [MBProgressHUD showInformation:@"请输入正确的银行卡号" toView:self.view andAfterDelay:HUD_TIME];
        return;
    }
    
    
    
    [self showLoadingAnimatedToView:self.view];
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        
        [self dismissLoadingAnimatedToView];
        
        HHYPayAlertView *alertView =  [HHYPayAlertView showPayAlert];
        
        __weak typeof(self)wSelf = self;
        alertView.handler = ^(BOOL res, NSString * _Nonnull reason) {
            __strong typeof(self)sSelf = wSelf;
            if (res) {
                [DGPUtil calculatemoney:@"提现" by:[weakSelf.moneyTextField.text floatValue] successCallback:^{
                    [weakSelf addRecord];
                    
                    GPWithdrawalResultVC *vc = [[GPWithdrawalResultVC alloc] init];
                    vc.navTitle = @"提现状态";
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                } failedCallback:^{
                    [MBProgressHUD showInformation:@"提现失败" toView:weakSelf.view andAfterDelay:HUD_TIME];
                }];
            } else {
                [MBProgressHUD showInformation:reason toView:weakSelf.view andAfterDelay:HUD_TIME];
            }
        };
        

        

    });
}

-(void)addRecord{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    GPAccountModel *model = [[GPAccountModel alloc] init];
    model.type = 2;
    model.money = self.moneyTextField.text;
    model.date = [dateFormatter stringFromDate:[NSDate date]];
    model.status = @"等待审核";
    NSMutableArray *muArr = [NSMutableArray arrayWithArray:[USER_DEFAULTS objectForKey:[NSString stringWithFormat:@"%@_topList",[DGPUtil getCurrentUserID]]]];
    [muArr addObject:model.mj_JSONObject];
    NSArray *array = muArr;
    [USER_DEFAULTS setObject:array forKey:[NSString stringWithFormat:@"%@_topList",[DGPUtil getCurrentUserID]]];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
